sql-insight
A toolkit for SQL query analysis, formatting, and transformation. Leveraging the comprehensive parsing capabilities of sqlparser-rs, it can handle various SQL dialects.
Features
- SQL Formatting: Format SQL queries to standardized form, improving readability and maintainability.
- SQL Normalization: Convert SQL queries into a normalized form, making them easier to analyze and process.
- Table Extraction: Extract tables referenced in SQL queries, clarifying the data sources involved.
- CRUD Table Extraction: Identify the create, read, update, and delete operations, along with the tables involved in each operation within SQL queries.
Installation
Add sql_insight
to your Cargo.toml
file:
[]
= { = "0.1.0" }
Usage
SQL Formatting
Format SQL queries according to different dialects:
use GenericDialect;
let dialect = GenericDialect ;
let formatted_sql = format.unwrap;
assert_eq!;
SQL Normalization
Normalize SQL queries to abstract away literals:
use GenericDialect;
let dialect = GenericDialect ;
let normalized_sql = normalize.unwrap;
assert_eq!;
Table Extraction
Extract table references from SQL queries:
use GenericDialect;
let dialect = GenericDialect ;
let tables = extract_tables.unwrap;
println!;
This outputs:
[Ok(Tables([TableReference { catalog: Some(Ident { value: "catalog", quote_style: None }), schema: Some(Ident { value: "schema", quote_style: None }), name: Ident { value: "users", quote_style: Some('`') }, alias: Some(Ident { value: "users_alias", quote_style: None }) }]))]
CRUD Table Extraction
Identify CRUD operations and the tables involved in each operation within SQL queries:
use GenericDialect;
let dialect = GenericDialect ;
let crud_tables = extract_crud_tables.unwrap;
println!;
This outputs:
[Ok(CrudTables { create_tables: [TableReference { catalog: None, schema: None, name: Ident { value: "users", quote_style: None }, alias: None }], read_tables: [TableReference { catalog: None, schema: None, name: Ident { value: "employees", quote_style: None }, alias: None }], update_tables: [], delete_tables: [] })]
Supported SQL Dialects
sql-insight
supports a comprehensive range of SQL dialects through sqlparser-rs. For details on supported dialects, please refer to the sqlparser-rs documentation.
Contributing
Contributions to sql-insight
are welcome! Whether it's adding new features, fixing bugs, or improving documentation, feel free to fork the repository and submit a pull request.
License
sql-insight
is distributed under the MIT license.